//
//  PersonalCenterViewController.m
//  NewPro
//
//  Created by RYX on 2018/10/11.
//  Copyright © 2018年 何荧红. All rights reserved.
//

#import "PersonalCenterViewController.h"
#import "PersonalCenterCell.h"
#import "PersonalCenterStore.h"
#import "UserList.h"
#import "UserModel.h"

@interface PersonalCenterViewController ()<UITableViewDelegate,UITableViewDataSource,UIImagePickerControllerDelegate,UINavigationControllerDelegate>

@property (strong, nonatomic) UIImage *theNewImg;
@property (nonatomic,copy) NSArray *titlesArr;
@property (nonatomic,strong) NSMutableArray *desArr;

@property (nonatomic,strong) UITableView *tableView;

@end

@implementation PersonalCenterViewController

#pragma mark - 模拟数据
- (NSArray *)titlesArr{
    
    if (!_titlesArr) {
        
        _titlesArr = @[@"我的头像",@"姓名",@"登录账号",@"推荐码",@"身份证",@"结算银行"];
        
    }
    
    return _titlesArr;
}

- (NSMutableArray *)desArr{
    
    if (!_desArr) {
        
         UserModel *model = [UserList sharedInstance].currentUserModel;
         
         NSString *headImage = @"无数据";
         if (model.headImage.length > 0) {
         
             headImage = model.headImage;
         }
         
         NSString *realName = @"无数据";
         if (model.realName.length > 0) {
         
             realName = model.realName;
         }
         
         NSString *customName = @"无数据";
         if (model.customName.length > 0) {
         
             customName = model.customName;
         }
         
         NSString *recommendationCode = @"无数据";
         if (model.customName.length > 0) {
         
             recommendationCode = model.customName;
         }
         
         NSString *idCardNo = @"未实名";
         if (model.idCardNo.length > 0) {
         
             
             idCardNo = [NSString stringWithFormat:@"%@*******%@",[model.idCardNo substringToIndex:4],[model.idCardNo substringFromIndex:model.idCardNo.length - 4]];
         }
        
        NSString *bankNo = @"未绑定";
        if (model.bankNo.length > 0) {
            
            bankNo = [NSString stringWithFormat:@"%@*******%@",[model.bankNo substringToIndex:4],[model.bankNo substringFromIndex:model.bankNo.length - 4]];
            
        }
        
         NSArray *arr = @[headImage,realName,customName,recommendationCode,idCardNo,bankNo];
        
        _desArr =  [[NSMutableArray alloc]initWithArray:arr];
    }
    
    return _desArr;
}

- (UITableView *)tableView{
    
    if (!_tableView) {
        
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView setTableFooterView:[[UIView alloc] initWithFrame:CGRectZero]];
    }
    
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor colorWithHexString:@"F1F1F1"];
    
    [self setNavBarWithTitle:@"个人中心"];
    
    [self.view addSubview:self.tableView];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.mas_equalTo(0);
        make.top.equalTo(self.view).offset(50);
        make.bottom.equalTo(self.view).offset(0);
    }];
}

#pragma mark - UITableViewDelegate,UITableViewDataSource
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return [PersonalCenterCell getHeightWithIndexPath:indexPath];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.titlesArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    PersonalCenterCell *cell = [PersonalCenterCell tempWithTableView:tableView andTitlesArr:self.titlesArr andIndexPath:indexPath];
    cell.rowOfCell = indexPath.row;
    [cell setTitleWithArr:self.titlesArr andDesArr:self.desArr];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    switch (indexPath.row) {
        case 0:
            
            [self selectImg];
            break;
            
        default:
            break;
    }
}

#pragma mark - 选择图片
- (void)selectImg{
    
    UIImagePickerController * picker = [[UIImagePickerController alloc]init];

    picker.delegate = self;

    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

    [self presentViewController:picker animated:YES completion:nil];
}

#pragma mark - 相册选择完成
-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    
    MJWeakSelf
    [picker dismissViewControllerAnimated:YES completion:^{
        
        UIImage *selectedImage=[info objectForKey:UIImagePickerControllerOriginalImage];
    
        [weakSelf.view makeToastActivity:CSToastPositionCenter];
        
        MJWeakSelf
        PersonalCenterStore *store = [[PersonalCenterStore alloc] init];
        [store postImgWithImg:[ImgManager imageToString:selectedImage] Success:^(NSString *imgUrl){
            
            [self->_desArr replaceObjectAtIndex:0 withObject:imgUrl];
            
            [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:[NSIndexPath indexPathForRow:0 inSection:0], nil] withRowAnimation:UITableViewRowAnimationNone];
            
            //通知合伙人页面刷新
            [[NSNotificationCenter defaultCenter] postNotificationName:@"refreshUI" object:nil];
            
            if (self.imgUpBlock) {
                
                self.imgUpBlock(imgUrl);
            }
            
            [weakSelf.view hideToastActivity];
            
        } Failture:^(NSError *error) {
           
            [weakSelf.view hideToastActivity];
            [weakSelf showMBPError:[HttpTool handleError:error]];
        }];
    
    }];
    
}




@end
